package juc.cas;

import sun.misc.Unsafe;

/**
 * @ClassName CASTest
 * @PackageName juc.cas
 * @projectName myThread
 * @Description 
 * @Author games
 * @Date 2025/8/29 上午10:04
 * @Version 1.0
 */
public class CASTest {
    public static void main(String[] args) {
        Entity entity = new Entity();
        Unsafe unsafe = UnsafeFactory.getUnsafe();
        long offset = UnsafeFactory.getFieldOffset(unsafe, Entity.class, "x");
        boolean successful = unsafe.compareAndSwapInt(entity, offset, 0, 3);
        System.out.println(successful + "\t" + entity.x);

        successful = unsafe.compareAndSwapInt(entity, offset, 3, 5);
        System.out.println(successful + "\t 是" + entity.x);

        successful = unsafe.compareAndSwapInt(entity, offset, 3, 8);
        System.out.println(successful + "\t" + entity.x);

    }
    static class Entity {
        public volatile int x;

    }
}